package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Novel;
import com.ruoyi.system.domain.Vo.BookshelfVo;
import com.ruoyi.system.service.INovelService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Bookshelf;
import com.ruoyi.system.service.IBookshelfService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 书架管理Controller
 * 
 * @author 许雅萍
 * @date 2024-11-19
 */
@Controller
@RequestMapping("/system/bookshelf")
public class BookshelfController extends BaseController
{
    private String prefix = "system/bookshelf";

    @Resource
    private ISysUserService userService;

    @Autowired
    private IBookshelfService bookshelfService;

    @Autowired
    private INovelService novelService;




    @GetMapping()
    public String bookshelf()
    {
        return prefix + "/bookshelf";
    }

    /**
     * 查询书架管理列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Bookshelf bookshelf)
    {
        startPage();
        List<Bookshelf> list = bookshelfService.selectBookshelfList(bookshelf);
        List<BookshelfVo> bookshelfVos = new ArrayList<>();
        list.forEach(bookshelf1 -> {
            Long userId = bookshelf1.getUserId();
            SysUser sysUser = userService.selectUserById(userId);
            BookshelfVo build = BookshelfVo.builder()
                    .id(bookshelf1.getId())
                    .name(bookshelf1.getName())
                    .userName(sysUser.getUserName())
                    .createTime(bookshelf1.getCreateTime())
                    .updateTime(bookshelf1.getUpdateTime())
                    .build();
            bookshelfVos.add(build);
        });
        return getDataTable(bookshelfVos);
    }

    /**
     * 导出书架管理列表
     */
    @RequiresPermissions("system:bookshelf:export")
    @Log(title = "书架管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Bookshelf bookshelf)
    {
        List<Bookshelf> list = bookshelfService.selectBookshelfList(bookshelf);
        ExcelUtil<Bookshelf> util = new ExcelUtil<Bookshelf>(Bookshelf.class);
        return util.exportExcel(list, "书架管理数据");
    }

    /**
     * 新增书架管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    @GetMapping("/addBook")
    public String addBook(ModelMap modelMap)
    {
        List<Novel> novels = novelService.selectNovelList(new Novel());
        modelMap.put("novels",novels);
        return prefix + "/addBook";
    }
    /**
     * 新增保存书架管理
     */
    @Log(title = "书架管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Bookshelf bookshelf)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        bookshelf.setUserId(sysUser.getUserId());
        bookshelf.setCreateTime(new Date());
        bookshelf.setUpdateTime(new Date());
        return toAjax(bookshelfService.insertBookshelf(bookshelf));
    }

    /**
     * 修改书架管理
     */

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Bookshelf bookshelf = bookshelfService.selectBookshelfById(id);
        mmap.put("bookshelf", bookshelf);
        return prefix + "/edit";
    }

    /**
     * 修改保存书架管理
     */

    @Log(title = "书架管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Bookshelf bookshelf)
    {
        bookshelf.setUpdateTime(new Date());
        return toAjax(bookshelfService.updateBookshelf(bookshelf));
    }

    /**
     * 删除书架管理
     */

    @Log(title = "书架管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bookshelfService.deleteBookshelfByIds(ids));
    }
}
